#usage : "perl perl_calculate_whole_vs_chopping.pl 4000000 0.0 T T"
$file_values = "whole_half_1_2_4_r2_clubbed.txt";
$file_coor = "coordinates";
#$edge = "1000000";
#$filter = 0.0;
$edge = $ARGV[0];
$filter =$ARGV[1];
$cord_ind = $ARGV[2];
#$avg = $ARGV[3];
$create_files = $ARGV[3];
$avg = "T";
chomp($edge);
chomp($filter);
chomp($cord_ind);
chomp($avg);
chomp($create_files);
if($edge eq "" || $filter eq "" || $cord_ind eq "")
{
	print "NO PROPER ARGUMENTS \nUSAGE : perl perl_calculate_whole_vs_chopping.pl edge filter coordIndicator CreateFiles\nEXAMPLE USAGE : perl perl_calculate_whole_vs_chopping.pl 4000000 0.0 T T\n";
}
#opening file if you want to create file
if($create_files eq "T" || $create_files eq "t")
{
	open(WRBUFF,">whole_half_1_2_4_$edge.txt");
}
open BUFF, "<$file_coor" or die "Can't open the file $file_coor";
open BUFF1, "<$file_values" or die "Can't open the file $file_values";
$k =0;
#capturing all coordinates in a array
while($line = <BUFF>)
{
	chomp($line);
	@array = split(/\t/,$line);
	$cord_array[$k] = $array[0] + $edge;
	$k++;
	$cord_array[$k] = $array[1] - $edge;
	$k++;

}
#storing initial and final coordinate separately
$intial = shift(@cord_array);
$final = pop(@cord_array);
#print "@cord_array\n";
$ind_half_quater_whole=0;
$ind_quater_whole =0;
$ind_half_whole = 0;
$ind_1_whole = 0;
$ind_2_whole = 0;
$ind_4_whole = 0;
$ind_whole_half_quater =0;
$ind_whole_quater =0;
$ind_whole_half = 0;
$ind_whole_1 = 0;
$ind_whole_2 = 0;
$ind_whole_4 = 0;
$avg_half_quater_whole=0;
$avg_quater_whole=0;
$avg_half_whole = 0;
$avg_1_whole = 0;
$avg_2_whole = 0;
$avg_4_whole = 0;
$avg_whole_half_quater =0;
$avg_whole_quater =0;
$avg_whole_half = 0;
$avg_whole_1 = 0;
$avg_whole_2 = 0;
$avg_whole_4 = 0;
$numb =0;
$numb1 =0;
#reading through main file
while($line =<BUFF1>)
{
	$numb1++;
	chomp($line);
        @array = split(/\t/,$line);
	$whole = $array[2];
	$pos = $array[1];
	$snp = $array[0];
	$mbhalfquater = $array[3];
	$mbquater = $array[4];
	$mbhalf = $array[5];
	$mb1 = $array[6];
	$mb2 = $array[7];
	$mb4 = $array[8];
	$indi =1;
	$indy =0;
#filtering applied if option selected based on edges
   if($cord_ind eq "T" || $cord_ind eq "t" )
     {
	$indi =0;
	if($pos < $intial+1 || $pos > $final-1)
	{
		$indi =1;
	}
	for($i=0;$i<@cord_array;$i++)
	{
		$lo1 = $cord_array[$i];
		$i++;
		$lo2 = $cord_array[$i];	
		if($pos < $lo2+1 && $pos > $lo1-1)
        	{
	                $indi =1;
			$indy = 2;
        	}

	}
	if($indy ==2)
	{
		$line =~ s/\s/\t/g;
		print WRBUFF $line."\n";
		$numb++;
	}
     }
	if($indi == 1)
	{
		if($whole ne "NaN" && $whole > $filter && $mbhalfquater eq "NaN")
                {
                        $ind_whole_half_quater++;
                        if($avg eq "T" || $avg eq "t")
                        {
                                $avg_whole_half_quater = $avg_whole_half_quater+$whole;
                        }
                }
		if($whole ne "NaN" && $whole > $filter && $mbquater eq "NaN")
                {
                        $ind_whole_quater++;
                        if($avg eq "T" || $avg eq "t")
                        {
                                $avg_whole_quater = $avg_whole_quater+$whole;
                        }
                }
		if($whole ne "NaN" && $whole > $filter && $mbhalf eq "NaN")
		{
			$ind_whole_half++;
			if($avg eq "T" || $avg eq "t")
			{
				$avg_whole_half = $avg_whole_half+$whole;
			}
		}
		if($whole ne "NaN" && $whole > $filter && $mb1 eq "NaN")
        	{
	
                	$ind_whole_1++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_whole_1 = $avg_whole_1+$whole;
                        }

        	}
		if($whole ne "NaN" && $whole > $filter && $mb2 eq "NaN")
        	{
                	$ind_whole_2++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_whole_2 = $avg_whole_2+$whole;
                        }

        	}
		if($whole ne "NaN" && $whole > $filter && $mb4 eq "NaN")
        	{
                	$ind_whole_4++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_whole_4 = $avg_whole_4+$whole;
                        }

        	}
		if($whole eq "NaN" && $mbhalfquater ne "NaN" && $mbhalfquater > $filter)
                {
                        $ind_half_quater_whole++;
                        if($avg eq "T" || $avg eq "t")
                        {
                                $avg_half_quater_whole = $avg_half_quater_whole+$mbhalfquater;
                        }

                }
		if($whole eq "NaN" && $mbquater ne "NaN" && $mbquater > $filter)
                {
                        $ind_quater_whole++;
                        if($avg eq "T" || $avg eq "t")
                        {
                                $avg_quater_whole = $avg_quater_whole+$mbquater;
                        }

                }
		if($whole eq "NaN" && $mbhalf ne "NaN" && $mbhalf > $filter)
		{
			$ind_half_whole++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_half_whole = $avg_half_whole+$mbhalf;
                        }

		}	
		if($whole eq "NaN" && $mb1 ne "NaN" && $mb1 > $filter)
        	{
                	$ind_1_whole++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_1_whole = $avg_1_whole+$mb1;
                        }
        	}
		if($whole eq "NaN" && $mb2 ne "NaN" && $mb2 > $filter)
        	{
                	$ind_2_whole++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_2_whole = $avg_2_whole+$mb2;
                        }
        	}
		if($whole eq "NaN" && $mb4 ne "NaN" && $mb4 > $filter)
        	{
               	 	$ind_4_whole++;
			if($avg eq "T" || $avg eq "t")
                        {
                                $avg_4_whole = $avg_4_whole+$mb4;
                        }
        	}
	}
}
print "whole!= NaN : $ind_whole_half_quater $ind_whole_quater $ind_whole_half $ind_whole_1 $ind_whole_2 $ind_whole_4\n";
print "whole == NaN : $ind_half_quater_whole $ind_quater_whole $ind_half_whole $ind_1_whole $ind_2_whole $ind_4_whole\n"; 	
if($ind_whole_half_quater == 0)
{
        $ind_whole_half_quater =1;
}
if($ind_whole_quater == 0)
{
        $ind_whole_quater =1;
}

if($ind_whole_half == 0)
{
	$ind_whole_half =1;
}
if($ind_whole_1 == 0)
{
        $ind_whole_1 =1;
}
if($ind_whole_2 == 0)
{
        $ind_whole_2 =1;
}
if($ind_whole_4 == 0)
{
        $ind_whole_4 =1;
}
if($ind_half_quater_whole == 0)
{
        $ind_half_quater_whole =1;
}
if($ind_quater_whole == 0)
{
        $ind_quater_whole =1;
}
if($ind_half_whole == 0)
{
	$ind_half_whole =1;
}
if($ind_1_whole == 0)
{
        $ind_1_whole =1;
}
if($ind_2_whole == 0)
{
        $ind_2_whole =1;
}
if($ind_4_whole == 0)
{
        $ind_4_whole =1;
}
$avg_whole_half_quater = $avg_whole_half_quater/$ind_whole_half_quater;
$avg_whole_quater = $avg_whole_quater/$ind_whole_quater;
$avg_whole_half = $avg_whole_half/$ind_whole_half;
$avg_whole_1 = $avg_whole_1/$ind_whole_1;
$avg_whole_2 = $avg_whole_2/$ind_whole_2;
$avg_whole_4 = $avg_whole_4/$ind_whole_4;
$avg_half_quater_whole  =$avg_half_quater_whole/$ind_half_quater_whole;
$avg_quater_whole  =$avg_quater_whole/$ind_quater_whole;
$avg_half_whole  =$avg_half_whole/$ind_half_whole;
$avg_1_whole = $avg_1_whole/$ind_1_whole;
$avg_2_whole = $avg_2_whole/$ind_2_whole;
$avg_4_whole = $avg_4_whole/$ind_4_whole;
print "AVG whole!= NaN : $avg_whole_half_quater $avg_whole_quater $avg_whole_half $avg_whole_1 $avg_whole_2 $avg_whole_4\n";
print "AVG whole == NaN : $avg_half_quater_whole $avg_quater_whole $avg_half_whole $avg_1_whole $avg_2_whole $avg_4_whole\n";
print "$numb\t$numb1\n";
